/*
 * Copyright 2010 Daniel Kurka
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.googlecode.mgwt.examples.showcase.client.activities.animation;

import java.util.Date;
import java.util.List;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.mgwt.dom.client.event.tap.HasTapHandlers;
import com.googlecode.mgwt.examples.showcase.client.AppController;
import com.googlecode.mgwt.examples.showcase.client.i18n.Labels;
import com.googlecode.mgwt.examples.showcase.client.model.AddressEnum;
import com.googlecode.mgwt.examples.showcase.client.model.FloorEnum;
import com.googlecode.mgwt.examples.showcase.client.model.SverityEnum;
import com.googlecode.mgwt.examples.showcase.client.model.ZahalEntryEnum;
import com.googlecode.mgwt.examples.showcase.shared.ServiceCallDTO;
import com.googlecode.mgwt.ui.client.MGWT;
import com.googlecode.mgwt.ui.client.MGWTStyle;
import com.googlecode.mgwt.ui.client.dialog.ConfirmDialog;
import com.googlecode.mgwt.ui.client.dialog.ConfirmDialog.ConfirmCallback;
import com.googlecode.mgwt.ui.client.dialog.Dialogs;
import com.googlecode.mgwt.ui.client.dialog.Dialogs.OptionCallback;
import com.googlecode.mgwt.ui.client.dialog.Dialogs.OptionsDialogEntry;
import com.googlecode.mgwt.ui.client.widget.Button;
import com.googlecode.mgwt.ui.client.widget.FormListEntry;
import com.googlecode.mgwt.ui.client.widget.HeaderButton;
import com.googlecode.mgwt.ui.client.widget.HeaderPanel;
import com.googlecode.mgwt.ui.client.widget.LayoutPanel;
import com.googlecode.mgwt.ui.client.widget.MListBox;
import com.googlecode.mgwt.ui.client.widget.ProgressIndicator;
import com.googlecode.mgwt.ui.client.widget.WidgetList;

/**
 * @author Daniel Kurka
 * 
 */
public class AnimationViewGwtImpl implements AnimationView {

	private LayoutPanel main;
	private HeaderPanel headerPanel;
	private HeaderButton headerBackButton;
	private Button postButton ;
//	private List<OptionsDialogEntry> confirmationButons;
	private ProgressIndicator progressIndicator;
//	MListBox buildingListBox = new MListBox();
	MListBox addressListBox = new MListBox();
	MListBox floorListBox = new MListBox();
	MListBox severityListBox = new MListBox();

	/**
	 * 
	 */
	public AnimationViewGwtImpl() {
		postButton = new Button(AppController.getClientFactory().getLabelResource().post());
		 progressIndicator = new ProgressIndicator();
		progressIndicator.getElement().setAttribute("style", "margin:auto; margin-top: 50px");
		main = new LayoutPanel();
		main.setStyleName("dir");
		headerPanel = new HeaderPanel();
		headerBackButton = new HeaderButton();
		initModel();
		initHeader();
		initView();
//		initConfirmationButtons();

	}

	

//	private void initConfirmationButtons() {
//		confirmationButons = new ArrayList<OptionsDialogEntry>();
//		confirmationButons.add(new OptionsDialogEntry(AppController.getClientFactory().getLabelResource().ok(), ButtonType.CONFIRM));
//		confirmationButons.add(new OptionsDialogEntry(AppController.getClientFactory().getLabelResource().cancel(), ButtonType.NORMAL));
//		
//	}



	private void initHeader() {
		headerPanel.setLeftWidget(headerBackButton);
		headerBackButton.setBackButton(true);
		headerBackButton.setVisible(!MGWT.getOsDetection().isAndroid());
		main.add(headerPanel);
		HTML header2 = new HTML(AppController.getClientFactory().getLabelResource().lampLocation());
		header2.addStyleName(MGWTStyle.getTheme().getMGWTClientBundle().getListCss().listHeader());
		main.add(header2);
		
	}

	private void initView() {
		final WidgetList widgetList = new WidgetList(); 
		Labels labels = AppController.getClientFactory().getLabelResource();
		widgetList.add(new FormListEntry(labels.address(), addressListBox));
		widgetList.add(new FormListEntry(labels.floor(), floorListBox));
		widgetList.add(new FormListEntry(labels.severity(), severityListBox));
		widgetList.setRound(true);
		main.add(widgetList);
		main.add(postButton);


	}

	private void initModel() {
		for (AddressEnum building  : AddressEnum.values()) {
			addressListBox.addItem(building.getDisplayName());
		}
		for (FloorEnum floor  : FloorEnum.values()) {
			floorListBox.addItem(floor.getDisplayName());
		}
		for (SverityEnum severirty  : SverityEnum.values()) {
			severityListBox.addItem(severirty.getDisplayName());
		}
	}



	
		
		
	private void initZahalModel(){
		for (ZahalEntryEnum entry  : ZahalEntryEnum.values()) {
			addressListBox.addItem(entry.getDisplayName());
		}
		
	}
	
	

	@Override
	public Widget asWidget() {
		return main;
	}

	@Override
	public void setTitle(String text) {
		headerPanel.setCenter(text);

	}

	@Override
	public HasTapHandlers getBackButton() {
		return headerBackButton;
	}

	

	@Override
	public void setLeftButtonText(String text) {
		headerBackButton.setText(text);

	}



	@Override
	public HasTapHandlers getPostButton() {
		return postButton;
	}



	@Override
	public ServiceCallDTO getServiceCallDTO() {
		ServiceCallDTO dto = new ServiceCallDTO();
		dto.setAddressID(addressListBox.getSelectedIndex());
		dto.setFloorID(floorListBox.getSelectedIndex());
		dto.setSeverityID(severityListBox.getSelectedIndex());
		dto.setDate(new Date());
		return dto;
	}



	@Override
	public boolean validate() {
//		if(buildingListBox.getSelectedIndex() < 0 ||
//				addressListBox.getSelectedIndex() < 0 ||
//				floorListBox.getSelectedIndex() < 0 ){
//			Dialogs.alert(AppController.getClientFactory().getLabelResource().error(), 
//					AppController.getClientFactory().getLabelResource().emptyFields(), null);
//			return false;
//		}
		return true;
	}



	@Override
	public void confirm(List<OptionsDialogEntry> list, OptionCallback callback) {
		Dialogs.options(list, callback, main);
	}

	@Override
	public void confirm(String title, String msg,ConfirmCallback confirmCallback) {
		ConfirmDialog confirmDialog = new ConfirmDialog(MGWTStyle.getTheme().getMGWTClientBundle().getDialogCss(),
				title, msg, confirmCallback,AppController.getClientFactory().getLabelResource().ok(),
				AppController.getClientFactory().getLabelResource().cancel());
		confirmDialog.show();
		
	}


	@Override
	public void setBussy(boolean b) {
		if(b){
			main.remove(postButton);
			main.add(progressIndicator);
		}
		else{
			main.remove(progressIndicator);
			main.add(postButton);
		}
		
	}

	@Override
	public void showDuplicateMsg(ServiceCallDTO dto) {
		Dialogs.alert(dto.getDisplayString(), 
				AppController.getClientFactory().getLabelResource().duplicateReport()+
				DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT).format(dto.getDate()), null);
		
		
	}





}
